package com.example.test.service;

import com.example.test.mapper.HolidayMapper;
import com.example.test.pojo.Holiday;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ymgu
 * @version V1.0
 * @Package com.example.test.service
 * @date 2022/6/15 15:38
 */
@Service
public class HolidayServiceImpl implements HolidayService{
    @Autowired
    HolidayMapper holidayMapper;

    @Override
    public String perWorkDate(Date date) throws ParseException {
        String checkDate=new SimpleDateFormat("yyyy-MM-dd").format(date);
        Holiday holiday=holidayMapper.selectHolidayByDate(checkDate);
        if(Objects.isNull(holiday)){
            return checkDate;
        }else{
            if(holiday.getHolidayWage()==1){
                return checkDate;
            }else{
                Date dateBefore = new SimpleDateFormat("yyyy-MM-dd").parse(dayBefore(checkDate));
                return perWorkDate(dateBefore);
            }
        }
    }

    @Override
    public String lateWorkDate(Date date) throws ParseException {
        String checkDate=new SimpleDateFormat("yyyy-MM-dd").format(date);
        Holiday holiday=holidayMapper.selectHolidayByDate(checkDate);
        if(Objects.isNull(holiday)){
            return checkDate;
        }else{
            if(holiday.getHolidayWage()==1){
                return checkDate;
            }else {
                Date dateAfter = new SimpleDateFormat("yyyy-MM-dd").parse(dayAfter(checkDate));
                return lateWorkDate(dateAfter);
            }
        }
    }

    @Override
    public void addHolidayAndWeekends(String year) {
        try {
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
            headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
            HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
            Object response = restTemplate.exchange("http://timor.tech/api/holiday/year/"+year+"?type=Y&week=Y", HttpMethod.GET,entity,Object.class);
            Object object= ((ResponseEntity) response).getBody();
            Map<String,Object> map= (Map) object;
            Map<String,Object> mapHoliday= (Map) map.get("holiday");
            Holiday holidayEntry=new Holiday();
            for (Map.Entry<String,Object> entry:mapHoliday.entrySet()){
                Map<String,Object> mapValue=(Map<String, Object>)entry.getValue();
                Boolean holiday= (Boolean) mapValue.get("holiday");
                String name= (String) mapValue.get("name");
                Integer wage= (Integer) mapValue.get("wage");
                String date = mapValue.get("date").toString();
                holidayEntry.setHoliday(holiday);
                holidayEntry.setHolidayName(name);
                holidayEntry.setHolidayWage(wage);
                holidayEntry.setHolidayDate(date);
                holidayMapper.insert(holidayEntry);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 获得参数日期的前一天
     * @param dateString
     * @return String
     */
    String dayBefore(String dateString) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        Date date=null;
        date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, day - 1);
        String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        return dayBefore;
    }

    /**
     * 获得参数日期的后面的后一天
     * @param dateString
     * @return String
     */
    String dayAfter(String dateString) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        Date date=null;
        date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, day + 1);
        String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        System.out.println(dayAfter);
        return dayAfter;
    }
}
